package net.halflite.mzkr.form.regist;

import java.io.Serializable;

import net.halflite.mzkr.entity.RegistedUser;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.seasar.framework.container.annotation.tiger.Component;
import org.seasar.framework.container.annotation.tiger.InstanceType;
import org.seasar.struts.annotation.Arg;
import org.seasar.struts.annotation.EmailType;
import org.seasar.struts.annotation.Mask;
import org.seasar.struts.annotation.Maxlength;
import org.seasar.struts.annotation.Minlength;
import org.seasar.struts.annotation.Msg;
import org.seasar.struts.annotation.Required;
import org.seasar.struts.annotation.Validwhen;

/** ユーザー登録用ActionForm */
@Component(instance = InstanceType.SESSION)
public class RegistUserForm implements Serializable {

	private static final long serialVersionUID = 1L;

	/** ユーザーID */
	@Required(arg0 = @Arg(key = "ユーザーID", resource = false))
	@Minlength(minlength = RegistedUser.MIN_USERNAME_LENGTH, 
			arg0 = @Arg(key = "ユーザーID", resource = false))
	@Maxlength(maxlength = RegistedUser.MAX_USERNAME_LENGTH, 
			arg0 = @Arg(key = "ユーザーID", resource = false))
	@Mask(mask = "^[0-9a-zA-Z_]+$", msg = @Msg(key = "errors.ng.username"))
	public String username;

	/** メールアドレス */
	@Required(arg0 = @Arg(key = "メールアドレス", resource = false))
	@Maxlength(maxlength = RegistedUser.MAX_ADDRESS_LENGTH,
			arg0 = @Arg(key = "メールアドレス", resource = false))
	@Mask(mask = "^\\S+$", msg = @Msg(key = "errors.include.space"),
			arg0 = @Arg(key = "入力したメールアドレス", resource = false))
	@EmailType(arg0 = @Arg(key = "入力したメールアドレス", resource = false))
	public String address;

	/** パスワード */
	@Required(arg0 = @Arg(key = "パスワード", resource = false))
	@Minlength(minlength = RegistedUser.MIN_PASSWORD_LENGTH, 
			arg0 = @Arg(key = "パスワード", resource = false))
	@Maxlength(maxlength = RegistedUser.MAX_PASSWORD_LENGTH, 
			arg0 = @Arg(key = "パスワード", resource = false))
	public String password;

	/** パスワード確認 */
	@Validwhen(test = "((password == null) or (*this* == password))",
			msg = @Msg(key = "errors.confirm.not.equal"),
			arg0 = @Arg(key = "パスワード", resource = false))
	public String confirm;
	
	/** 同意事項 */
	@Validwhen(test = "(*this* == 'true')",
			msg = @Msg(key = "errors.not.agree"),
			arg0 = @Arg(key = "規約", resource = false))
	public boolean agree;
	
	public void reset() {
		agree = false;
	}

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
